package com.lxn.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.lxn.course.domain.Pager;
import com.lxn.feignclients.FastdfsFeignClient;
import com.lxn.hrm.constants.MQConstants;
import com.lxn.hrm.feignclients.RedisFeignClient;
import com.lxn.hrm.util.AjaxResult;
import com.lxn.hrm.util.FileUtils;
import com.lxn.hrm.util.ZipUtils;
import com.lxn.hrm.util.myFileUtils;
import com.lxn.page.mapper.PagerMapper;
import com.lxn.page.mapper.SiteMapper;
import com.lxn.page.service.IPagerService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lxn.utils.VelocityUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hwerth
 * @since 2020-02-26
 */
@Service
public class PagerServiceImpl extends ServiceImpl<PagerMapper, Pager> implements IPagerService {
    @Autowired
    private FastdfsFeignClient fastdfsFeignClient;
    @Autowired
    private RedisFeignClient redisFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SiteMapper siteMapper;

    private String getTempDir(){
        String tempdir = System.getProperty("java.io.tmpdir");
        return tempdir;
    }
    @Override
    public void pageStatic(String pageName, String redisDataKey) throws Exception {
        //判断基本参数
        if(!StringUtils.hasLength(pageName)||!StringUtils.hasLength(redisDataKey)){
            throw new RuntimeException("页面静态化参数异常!");
        }
        //根据pageName查询Pager对象
        Pager pager = baseMapper.selectByName();
        //从fastdfs下载模板
        byte[] templateZipBytes = fastdfsFeignClient.download(pager.getTemplateUrl());
        if(templateZipBytes == null||templateZipBytes.length == 0){
            throw new RuntimeException("模板下载失败!");
        }
        //写入磁盘，临时目录
        String zipPath = getTempDir()+pageName+"home.zip";
        File zipFile = myFileUtils.createFile(zipPath);
        FileCopyUtils.copy(templateZipBytes,zipFile);
        //解压
        String unZipDir = getTempDir()+"pageStatic";
        ZipUtils.unZip(zipPath,unZipDir);
        //准备数据
        AjaxResult ajaxResult = redisFeignClient.get(redisDataKey);
        if(!ajaxResult.isSuccess()||ajaxResult.getResultObj()==null){
            throw new RuntimeException("数据获取失败!");
        }
        String dataString = ajaxResult.getResultObj().toString();
        Map<String,Object> model = JSON.parseObject(dataString,Map.class);
        model.put("staticRoot",unZipDir);
        //解压后模板路径
        String templatePath = unZipDir+pageName+".vm";
        //保存路径
        String htmlPath = unZipDir+pageName+".html";
        VelocityUtils.staticByTemplate(model,templatePath,htmlPath);
        //html上传到fastdfs
        byte[] htmlBytes = FileCopyUtils.copyToByteArray(new File(htmlPath));

        AjaxResult htmlUploadAjaxResult = fastdfsFeignClient.uploadBytes(htmlBytes, "html");

        if(!htmlUploadAjaxResult.isSuccess()||htmlUploadAjaxResult.getResultObj()==null){
            throw new RuntimeException("页面静态化:html上传失败!");
        }
        String htmlPathFastdfs = htmlUploadAjaxResult.getResultObj().toString();
        //MQ发消息
        String physicalPath = pager.getPhysicalPath();

        Map<String,String> mqMessage = new HashMap<>();


        mqMessage.put("htmlPathFastdfs",htmlPathFastdfs);
        mqMessage.put("physicalPath",physicalPath);

        String routingKey = siteMapper.selectById(pager.getSiteId()).getSn();
        rabbitTemplate.convertAndSend(MQConstants.NAME_EXCHANGE_PAGE_DIRECT,routingKey,mqMessage);
    }
}
